
#include "sg4c.h"
#include <time.h>

int x, y;
sg_font_t font;
void setup(){
    srand(time(NULL));

    x=0;
    y=0;

    font = sgCreateFont("Arial", 12);
}

void draw(){

    //~ x+=rand()%10;
    //~ y+=rand()%10;

    x%=sgWidth();
    y%=sgHeight();

    sgNoStroke();
    sgFill( sgGray(0x66) );
    sgRect(x, y, 20, 20);

    sgStrokeWeight(1.5f);
    sgStroke( sgGray(0x50) );
    sgNoFill();
    sgEllipse(x, y, 20, 20);

    sgStrokeWeight(5);
    sgLine(0, 0, 400, 200 );

    sgStrokeWeight(2);
    sgPoint(rand()%sgWidth(), rand()%sgHeight());

    sgStrokeWeight(1);
    sgFill( sgHexRGB(0xff0000) );
    sgPolygon(3, 10, 10, 20, 20, 5, 30 );

    int xpos[] = {25, 50, 25};
    int ypos[] = {25, 25, 50};
    sgPolygonArray(xpos, ypos, sizeof(xpos)/sizeof(int) );



    sgFont(font);
    sgStroke( sgHexRGB( 0x00FF00 ) );
    int i;
    for( i=0; i<5; ++i ){
        sgText("hello world!", 10, font.height*i);

    }

    sgRotate(1.0f/360);
}

void mouseMoved(){
    x = sgMouse().x;
    y = sgMouse().y;
    fprintf( stderr, " mouse moved %d %d\n", x, y );
}

void mousePressed(){
    x = sgMouse().x;
    y = sgMouse().y;
    int button = sgMouse().button;
    fprintf( stderr, " mouse pressed %d %d %d\n", x, y, button );
}

void mouseReleased(){
    x = sgMouse().x;
    y = sgMouse().y;
    int button = sgMouse().button;
    fprintf( stderr, " mouse released %d %d %d\n", x, y, button );
}

void mouseClicked(){
    x = sgMouse().x;
    y = sgMouse().y;
    int button = sgMouse().button;
    fprintf( stderr, " mouse clicked %d %d %d\n", x, y, button );
}

void mouseDragged(){
    x = sgMouse().x;
    y = sgMouse().y;
    int button = sgMouse().button;
    fprintf( stderr, " mouse dragged %d %d %d\n", x, y, button );
}

void mouseEntered(){
    x = sgMouse().x;
    y = sgMouse().y;
    fprintf( stderr, " mouse entered %d %d\n", x, y );
}

void mouseExited(){
    x = sgMouse().x;
    y = sgMouse().y;
    fprintf( stderr, " mouse exit %d %d\n", x, y );
}

void keyReleased(){
    char keychar = sgKey().character;
    int code = sgKey().code;
    fprintf( stderr, " key released %c %x\n", keychar, code );
}

void keyPressed(){
    char keychar = sgKey().character;
    int code = sgKey().code;
    fprintf( stderr, " key pressed %c %x\n", keychar, code );
}

void keyTyped(){
    char keychar = sgKey().character;
    int code = sgKey().code;
    fprintf( stderr, " key typed %c %x\n", keychar, code );
}

int main(){
    sg_config_t config;
    sgInitConfig( &config );

    config.screen.width = 400;
    config.screen.height = 200;
    config.framerate = 30;

    //config.smooth = 1;

    config.setup = setup;
    config.draw = draw;
    config.mousePressed = mousePressed;
    config.mouseReleased = mouseReleased;
    config.mouseMoved = mouseMoved;
    config.mouseEntered = mouseEntered;
    config.mouseExited = mouseExited;
    config.mouseClicked = mouseClicked;
    config.mouseDragged = mouseDragged;

    config.keyReleased = keyReleased;
    config.keyPressed = keyPressed;
    config.keyTyped = keyTyped;

    sgSetup(&config);

    return sgRunForever();
}
